/**
 * 
 */

package daytrader.framework;
import java.io.*;
import javax.swing.JFrame;
import java.util.ArrayList;

import daytrader.common.Constants;
import daytrader.common.Utility;
import daytrader.gui.GUI;

/**
 * @author jay
 *
 */
public class App {

	public static void printInstructions()
	{
		System.out.println("Usage: ");
		System.out.println("  java App [-c user_config_file] [-d data_path] [-o observe_list_path] [-h]");
		return;
	}

	public static String parseArgUserConfig(String [] args)
	{
		int i=0;
		String arg;
		for (i=0; i < args.length; i++) {
			arg = args[i];
			if (arg.equals("-c"))
				return args[i+1];
		}
		return null;
	}

	public static String parseArgDataPath(String [] args)
	{
		int i=0;
		String arg;
		for (i=0; i < args.length; i++) {
			arg = args[i];
			if (arg.equals("-d"))
				return args[i+1];
		}
		return null;
	}
	public static String parseArgObserveFilePath(String [] args)
        {
		int i=0;
		String arg;
		for (i=0; i < args.length; i++) {
			arg = args[i];
			if (arg.equals("-o"))
				return args[i+1];
		}
                return null;
        }

	public static boolean parseArgHelp(String [] args)
	{
		int i=0;
		String arg;
		for (i=0; i < args.length; i++) {
			arg = args[i];
			if (arg.equals("-h"))
				return true;
		}
		return false;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if (App.parseArgHelp(args)) {
			printInstructions();
			System.exit(0);
		}
		String userConfigFileName = App.parseArgUserConfig(args);
		if (userConfigFileName == null) {
			userConfigFileName = Constants.USER_CONFIG_FILE_NAME;
		}
		String dataPath = Utility.getDataPath(App.parseArgDataPath(args));
		
		String observeListFileName = App.parseArgObserveFilePath(args);
		if (observeListFileName == null) {
			observeListFileName = Constants.DEFAULT_OBSERVE_LIST_PATH;
		}

		// create the engine
        Engine app = new Engine(userConfigFileName, dataPath, observeListFileName);

        // Start market ticking
		Tick tick = new Tick();
		tick.Start();

                ArrayList<JFrame> frames = new ArrayList<JFrame>();
                for (String observingStockName: app.getObservingStockNameList()) {
                    // create GUI window
                    JFrame f = new JFrame();
                    GUI gui = new GUI(app);
                    gui.setObservingStockName(observingStockName);
                    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    f.add(gui);
                    f.setSize(600,600);
                    f.setLocation(0,0);
                    f.setVisible(true);
                    frames.add(f);
                }
                app.registerRepaintNotifier(new RepaintNotifier(frames));
		// start the engine
		app.run();
		
		
		// data sample code
//		DataAPI data  =new DataModel("./src/daytrader/data");
//		TickData[] tick = data.getTickData("foo");
//		System.out.println(tick[0].open);
	}

}
